﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MadProps
{
    public abstract class PropModelOwner : PropOwner
    {
        readonly IList<PropModel> propModels;
        readonly Composition composition;

        protected PropModelOwner()
            : this(Composition.Default())
        {
        }

        protected PropModelOwner(Composition composition)
            : base(composition)
        {
            this.composition = composition.ThrowIfNull("composition");

            var list = new List<PropModel>();

            foreach (var prop in this.GetProps())
            {
                var pmType = typeof(PropModel<>).MakeGenericType(prop.Type);
                var model = Activator.CreateInstance(pmType, composition, prop) as PropModel;
                list.Add(model);
            }

            this.propModels = list.AsReadOnly();
        }

        public IEnumerable<PropModel> GetPropModels()
        {
            return propModels;
        }

        public PropModel GetPropModel(IHavePropId template)
        {
            return propModels.Single(pm => pm.Is(template));
        }
    }
}
